package org.leviatan.dataharbour.core.model.control;

import org.leviatan.common.keyutils.KeyGenerator;
import org.leviatan.dataharbour.core.model.PersistenceResource;
import org.leviatan.dataharbour.core.model.impl.DbPersistenceResourceImpl;
import org.leviatan.dataharbour.core.model.impl.FilePersistenceResourceImpl;
import org.leviatan.dataharbour.core.model.impl.HibernatePersistenceResourceImpl;
import org.w3c.dom.Element;

public abstract class PersistenceResourceHandler {
	
	
	/*
	public static void updatePersistenceResource(String id, String name, String hibConf) {
		
		
	}
	
	public static void updateHibernatePersistenceResource(String id, String name, String hibConf) {
		
	}
	
	*/
	
	public static PersistenceResource createPersistenceResource(String name, String type) {
		PersistenceResource p = null;
		if (type.equals("hibernate")) {
			p = new HibernatePersistenceResourceImpl();
			p.setName(name);
			p.setType(type);
		}
		
		if (p != null) p.setId(KeyGenerator.generateUniqueKeyAsString());
		
		return p;
	}

	
	public static HibernatePersistenceResourceImpl createHibernatePersistenceResource(
			String name, String confStr) {
		
		HibernatePersistenceResourceImpl hpr = new HibernatePersistenceResourceImpl();
		hpr.setId(KeyGenerator.generateUniqueKeyAsString());
		hpr.setName(name);
		hpr.setType("hibernate");
		hpr.setConfString(confStr);
		
		return hpr;
	}
	
	public static PersistenceResource instantiateModel(Element e) {
		
		String id = e.getElementsByTagName("id").item(0).getTextContent();
		String name = e.getElementsByTagName("name").item(0).getTextContent();
		String type = e.getElementsByTagName("type").item(0).getTextContent();
		
		PersistenceResource pr = null;
		if (type.equals("hibernate")) {
			
			String confString = e.getElementsByTagName("conf-string").item(0).getTextContent();
			pr = new HibernatePersistenceResourceImpl();
			((HibernatePersistenceResourceImpl) pr).setConfString(confString);
			
		} else if (type.equals("db")) {
			String dbName = e.getElementsByTagName("db-name").item(0).getTextContent();
			String dbUrl = e.getElementsByTagName("db-url").item(0).getTextContent();
			String jndiName = e.getElementsByTagName("jndi-name").item(0).getTextContent();
			String tblName = e.getElementsByTagName("table-name").item(0).getTextContent();
			String usrName = e.getElementsByTagName("user-name").item(0).getTextContent();
			String psw = e.getElementsByTagName("user-pass").item(0).getTextContent();
			
			pr = new DbPersistenceResourceImpl();
			((DbPersistenceResourceImpl) pr).setDBName(dbName);
			((DbPersistenceResourceImpl) pr).setDBUrl(dbUrl);
			((DbPersistenceResourceImpl) pr).setJndiName(jndiName);
			((DbPersistenceResourceImpl) pr).setTableName(tblName);
			((DbPersistenceResourceImpl) pr).setUserName(usrName);
			((DbPersistenceResourceImpl) pr).setUserPassword(psw);
			
		} else if (type.equals("file")) {
			String filePath = e.getElementsByTagName("file-path").item(0).getTextContent();
			
			pr = new FilePersistenceResourceImpl();
			((FilePersistenceResourceImpl) pr).setFilePath(filePath);
		}
		
		if (pr != null) {
			pr.setId(id);
			pr.setName(name);
			pr.setType(type);
		}
		return pr;
	}
	
	public static String convertToXml(PersistenceResource pr) {
		String xml = "";
		// append general info
		xml += "\n\t\t<id><![CDATA[" + pr.getId() + "]]></id>";
		xml += "\n\t\t<name><![CDATA[" + pr.getName() + "]]></name>";
		xml += "\n\t\t<type><![CDATA[" + pr.getType() + "]]></type>";
		
		// get specific info
		if (pr instanceof HibernatePersistenceResourceImpl) {
			xml += PersistenceResourceHandler.createHibernateSpecificXml(
					(HibernatePersistenceResourceImpl) pr);			
		}
		else if (pr instanceof FilePersistenceResourceImpl) {
			xml += PersistenceResourceHandler.createDbSpecificXml(
					(DbPersistenceResourceImpl) pr);	
		}
		else if (pr instanceof DbPersistenceResourceImpl) {
			xml += PersistenceResourceHandler.createFileSpecificXml(
					(FilePersistenceResourceImpl) pr);
		}
		return xml;
	}
	
	private static String createHibernateSpecificXml(HibernatePersistenceResourceImpl hpr) {
		String xml = "\n\t\t<conf-file><![CDATA[" + hpr.getConfString() + "]]></conf-file>";
		return xml;
	}
	
	private static String createDbSpecificXml(DbPersistenceResourceImpl dbp) {
		String xml = "\n\t\t<db-name><![CDATA[" + dbp.getDBName() + "]]></db-name>";
		xml += "\n\t\t<db-passw><![CDATA[" + dbp.getUserPassword() + "]]></db-passw>";
		xml += "\n\t\t<db-user><![CDATA[" + dbp.getUserName() + "]]></db-user>";
		xml += "\n\t\t<db-table><![CDATA[" + dbp.getTableName() + "]]></db-table>";
		xml += "\n\t\t<db-url><![CDATA[" + dbp.getDBUrl() + "]]></db-url>";
		xml += "\n\t\t<db-jndi-name><![CDATA[" + dbp.getJndiName() + "]]></db-jndi-name>";
		return xml;
	}
	
	private static String createFileSpecificXml(FilePersistenceResourceImpl fpr) {
		String xml = "\n\t\t<file-path><![CDATA[" + fpr.getFilePath() + "]]></file-path>";
		return xml;
	}
}
